/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package utils;

/**
 *Klasa opisująca wektor dwuwymiarowy
 * @author m
 */
public class Vector2D {

    public double x = 0; 
    public double y = 0;
    
    public Vector2D( double x, double y )
    {
        this.x = x;
        this.y = y;
    }
    
    
    public Vector2D add (Vector2D vector)
    {
        return new Vector2D(this.x + vector.x, this.y + vector.y);
    }
    
    public Vector2D subtract (Vector2D vector)
    {
        return new Vector2D (this.x - vector.x, this.y - vector.y);
    }
    
    public Vector2D multiple (double m)
    {
        return new Vector2D((m*this.x), (m*this.y));
    }
    
    public Vector2D divide (double m)
    {
        return new Vector2D((this.x/m),(this.y/m));
    }
    
    
    public double length ()
    {
        return Math.sqrt(this.x*this.x + this.y*this.y);
    }
    
    public Vector2D normalize ()
    {
        return new Vector2D((this.x/this.length()), (this.y/this.length()));
    }
    
    public double angle (Vector2D zaczepienie)
    {
        double angle = Math.acos((this.normalize()).dot(new Vector2D(0, -1.0).normalize()));
        if ( this.normalize().x < 0 )
        {
            angle = 2*Math.PI - angle;
           // System.out.println("a");
        }
        return angle;
    }
    
    public double dot(Vector2D v)
    {
        return this.x*v.x + this.y*v.y;
    }
    
    public Vector2D cutTo(double length)
    {
        if (this.length() > length)
        {
            return this.normalize().multiple(length);
        }
        else
        {
            return this;
        }
    }
    
    public boolean compare(Vector2D v)
    {
        if (this.x == v.x && this.y == v.y)
        {
            return true;
        }
        return false;
    }
    
    
    public Vector2D rotate(double angle)
    {
        //double ix = this.x * Math.cos(angle) + (this.y )* Math.sin(angle);
        //double iy = this.x * (-Math.sin(angle)) + (this.y) * Math.cos(angle);
        double ix = this.x * Math.cos(angle) + (this.y )* (-Math.sin(angle));
        double iy = this.x * Math.sin(angle) + (this.y) * Math.cos(angle);
        return new Vector2D(ix,iy);
    }
    
}
